#include "Circle.h"


void DrawCircle(SDL_Renderer* renderer, int centerX, int centerY, int radius) {
    int x = radius - 1;
    int y = 0;
    int dx = 1;
    int dy = 1;
    int err = dx - (radius << 1);

    while (x >= y) {
        SDL_RenderDrawPoint(renderer, centerX + x, centerY + y);
        SDL_RenderDrawPoint(renderer, centerX + y, centerY + x);
        SDL_RenderDrawPoint(renderer, centerX - y, centerY + x);
        SDL_RenderDrawPoint(renderer, centerX - x, centerY + y);
        SDL_RenderDrawPoint(renderer, centerX - x, centerY - y);
        SDL_RenderDrawPoint(renderer, centerX - y, centerY - x);
        SDL_RenderDrawPoint(renderer, centerX + y, centerY - x);
        SDL_RenderDrawPoint(renderer, centerX + x, centerY - y);

        if (err <= 0) {
            y++;
            err += dy;
            dy += 2;
        }
        if (err > 0) {
            x--;
            dx += 2;
            err += dx - (radius << 1);
        }
    }
}


Circle::Circle(float x, float y, float mass, float radius)
{
	this->position = vec2(x, y);
	this->velocity = vec2(0, 0);
    this->mass = mass;
	this->radius = radius;
	type = CIRCLE;

	rotation = glm::angleAxis(glm::radians(0.0f), glm::vec3(1, 0, 0));
	axis = vec3(0, 0, 0);
}





void Circle::applyForce(Force f)
{
	this->forces.push_back(f);
	
}


void Circle::update(float dt,SDL_Renderer* renderer)
{
	

	
	for (auto& f : forces)
	{
        this->velocity +=f.getForce()/mass;

		if (f.getType()==0)
		{
			forces.erase(std::find(forces.begin(), forces.end(), f));
		}

		
		
	}
	
	if (position.x + radius >= 800 || position.x - radius <= 0)
	{
		this->velocity.x *= -1;
		if (position.x + radius > 800)
		{
			position.x = 800 - radius;

		}
		if (position.x - radius < 0)
		{
			position.x = radius;
		}
		
	}
	if (position.y + radius >= 600 || position.y - radius <= 0)
	{
		this->velocity.y *= -1;
		if (position.y + radius > 600)
		{
			position.y = 600 - radius;
		}
		if (position.y - radius < 0)
		{
			position.y = radius;
		}
		
	}

	this->position += this->velocity*dt;
	this->axis=glm::axis(rotation);
  SDL_SetRenderDrawColor(renderer,255,0,0,255);
  //DrawCircle(renderer,position.x,position.y,radius);
  circleColor(renderer,position.x,position.y,radius,0xFFFFFFFF);


}

Rect Circle::getRect()
{
	
	return Rect(position-radius,position+radius);
}



vec2 Circle::getPosition() {
	return this->position;
}

vec2 Circle::getVelocity()
{
	return velocity;
}

float Circle::getMass()
{
	return mass;
}

void Circle::setPosition(float x, float y)
{
	position = vec2(x, y);
}

void Circle::setVelocity(vec2 v)
{
	velocity = v;
}

